﻿using System;
using System.Windows.Input;
using System.Diagnostics;
namespace ZC.MUSIC.COMMAND
{
    public class ResourceTestCommand : ICommand
    {

        readonly Action _execute;
        readonly Func<bool> _canExecute;
        public ResourceTestCommand(Action execute)
        {
            if (execute != null)
            {
                _execute = execute;
            }
        }

        public ResourceTestCommand(Action execute,Func<bool> canExecute)
        {
            if(execute!= null)
            {
                _execute = execute;
            }
            if (canExecute != null)
            {
                _canExecute = canExecute;
            }
        }
        

        event EventHandler ICommand.CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }

            remove
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        public void Execute(object parameter)
        {
            _execute?.Invoke();
        }

        
    }
}
